package org.akomantoso.core.data;

import java.io.IOException;

import org.akomantoso.core.data.type.VersionType;
import org.akomantoso.core.io.Externalizable;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.InputConstants;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.visitor.AKVisitor;

/**
 * the type amendmentStructure specifies the overall content model of the document types that describe amendments.
 * <pre>
 * <xsd:complexType name="amendmentStructure">
 * 		<xsd:sequence>
 * 			<xsd:element ref="meta" />
 * 			<xsd:element ref="coverPage" minOccurs="0" maxOccurs="1" />
 * 			<xsd:element ref="preface" minOccurs="0" maxOccurs="1" />
 * 			<xsd:element ref="amendmentBody" />
 * 			<xsd:element ref="conclusions" minOccurs="0" maxOccurs="1" />
 * 			<xsd:element ref="attachments" minOccurs="0" maxOccurs="1" />
 * 		</xsd:sequence>
 * 		<xsd:attribute name="contains" type="versionType" default="originalVersion" />
 * </xsd:complexType>
 * <pre>
 * 
 * 
 */
public abstract class AmendmentStucture implements Externalizable{

	public static final String ATTRIBUTE_CONTAINS = "contains";
	// Mandatory
	private final Meta _meta = new Meta();
	
	// Optional
	private CoverPage _coverPage;
	
	// Optional
	private Preface _preface;
	
	// Mandatory
	private final AmendmentBody _amendmentBody = new AmendmentBody();
	
	private VersionType _contains;
	
	public Meta getMeta() {
		return _meta;
	}

	public CoverPage getCoverPage() {
		return _coverPage;
	}

	public void setCoverPage(CoverPage coverPage) {
		_coverPage = coverPage;
	}

	public Preface getPreface() {
		return _preface;
	}

	public void setPreface(Preface preface) {
		_preface = preface;
	}

	public AmendmentBody getAmendmentBody() {
		return _amendmentBody;
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	public <T extends Output> void writeExternal(T out) throws IOException {
		
		if (_contains != null) {
			out.writeAttribute(ATTRIBUTE_CONTAINS, _contains.name());
		}
		
		_meta.writeExternal(out);
		if (_coverPage != null) {
			_coverPage.writeExternal(out);
		}
		
		if (_preface != null) {
			_preface.writeExternal(out);
		}
		_amendmentBody.writeExternal(out);
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	public <T extends Input> void readExternal(T in) throws IOException {
		
		_contains = VersionType.checkValueOf(in.readAttribute(ATTRIBUTE_CONTAINS));
		in.readNextStartElement();
		
		if (Meta.ELEMENT == in.getLocalName()) {
			_meta.readExternal(in);
		}
		
		if (CoverPage.ELEMENT == in.getLocalName()) {
			_coverPage = new CoverPage();
			_coverPage.readExternal(in);
		}
		
		if (InputConstants.END_ELEMENT == in.getEventType()) {
			in.readNextStartElement();	
		}
		
		if (Preface.ELEMENT == in.getLocalName()) {
			_preface = new Preface();
			_preface.readExternal(in);
		}
		
		if (InputConstants.END_ELEMENT == in.getEventType()) {
			in.readNextStartElement();	
		}
		
		if (AmendmentBody.ELEMENT == in.getLocalName()) {
			_amendmentBody.readExternal(in);
		}
	}

	
	protected final void internalAccept(AKVisitor visitor) {
		_meta.accept(visitor);
		if (_coverPage != null) {
			_coverPage.accept(visitor);
		}
		if (_preface != null) {
			_preface.accept(visitor);
		}
		_amendmentBody.accept(visitor);
	}
	
}
